package com.hypo.hystrixservice.controller;

import com.hypo.hystrixservice.command.GetUserInfoCommand;
import com.hypo.hystrixservice.command.GetUserInfoListCommand;
import com.hypo.hystrixservice.domain.UserInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import rx.Observable;
import rx.Observer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Future;

/**
 * 缓存服务的接口
 * @author Administrator
 *
 */
@Controller
public class CacheController {

	@RequestMapping("/change/user")
	@ResponseBody
	public UserInfo changeUser(String userId) {
		GetUserInfoCommand getUserInfoCommand = new GetUserInfoCommand(userId);
		UserInfo response = getUserInfoCommand.execute();
		return response;
	}
	@RequestMapping("/getList")
	@ResponseBody
	public List<UserInfo> getList() {
		List<String> userIds = Arrays.asList("1", "2", "3");
		GetUserInfoListCommand getUserInfoListCommand = new GetUserInfoListCommand(userIds);
		Observable<UserInfo> observe = getUserInfoListCommand.observe();
		ArrayList<UserInfo> userInfos = new ArrayList<>();
		observe.subscribe(new Observer<UserInfo>() {
			@Override
			public void onCompleted() {

			}

			@Override
			public void onError(Throwable throwable) {

			}

			@Override
			public void onNext(UserInfo userInfo) {
				userInfos.add(userInfo);
			}
		});

		return userInfos;
	}

}
